function $(id) {
    return document.getElementById(id)
}

window.onload = function () {
    findByItem(1);
}
/**
 * 动态条件分页查询
 * @param pageNO 页码
 */
function findByItem(pageNo) {
    axios.get("/room/findByItem",{
        params: {
            pageNo:pageNo,
            address:$("address").value,
            startPrice:$("startPrice").value,
            endPrice:$("endPrice").value,
        }
    }).then(resp =>{
        var str = "";
        var list = resp.data.list;
        list.forEach(e=>{
            str += `<tr><td>${e.address}</td><td>${e.price}</td>
                    <td>${e.createDate}</td><td>
                    <input type="button" value="删除" onclick="delRoom(${e.id})">
                    <input type="button" value="修改" onclick="showUpdate(${e.id})">
                    </td></tr>`
            $("roomData").innerHTML = str;
        })
        var pageStr ="";
        for (var i=1;i<=resp.data.pages;i++){
            pageStr += `<a style="margin-left: 10px" href="javascript:findByItem(${i})">${i}</a>`
            $("pageDiv").innerHTML = pageStr;
        }
    })
}

/**
 * 显示添加房间页面
 */
function showAddDiv() {
    $("addDiv").style.visibility = "visible";
    $("addDiv").style.width = document.documentElement.clientWidth+"px";
    $("addDiv").style.height = document.documentElement.clientHeight+"px";
}

/**
 * 添加房间
 */
function addRoom() {
    axios.get("/room/add",{
        params: {
            address:$("addressInput").value,
            price:$("priceInput").value,
            createDate:$("dateInput").value
        }
    }).then(resp =>{
        if (resp.data=="ok"){
            findByItem(1);
            $("addressInput").value='';
            $("priceInput").value='';
            $("dateInput").value='';
            $("addDiv").style.visibility = "hidden";
        }
    })
}

/**
 * 删除房间
 * @param roomId 房间id
 */
function delRoom(id) {
    axios.get("/room/del",{
        params:{
            id:id
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findByItem(1);
        }
    })
}

/**
 * 按ID查询，显示修改对话框
 * @param id 房间ID
 */
function showUpdate(id) {
    axios.get("/room/findById",{
        params:{
            id:id
        }
    }).then(resp=>{
        var obj = resp.data;
        $("roomId").value = obj.id;
        $("addressSpan").innerHTML=obj.address;
        $("newPrice").value= obj.price;
        $("dateSpan").innerHTML= obj.createDate;
        $("updateDiv").style.visibility = "visible";
        $("updateDiv").style.width = document.documentElement.clientWidth+"px";
        $("updateDiv").style.height = document.documentElement.clientHeight+"px";
    })

}

/**
 * 修改
 */
function updateRoom() {
    axios.get("/room/update",{
        params:{
            id:$("roomId").value,
            price:$("newPrice").value
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findByItem(1)
            $("updateDiv").style.visibility = "hidden";
        }
    })
}


